package com.bringway.asset.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.asset.constant.OtherConstant;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.p2p.service.CjService;

import common.Logger;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/other")
public class OtherController extends JsonController {
	
	private Logger logger = Logger.getLogger(OtherController.class);
	
	private static final String ROOT_PATH="e:/cj/";//图片存储文件夹
	
	@Autowired
	private CjService cjService;

	@RequestMapping("/znnh")
	public ModelAndView main() {
		JSONObject params = new JSONObject();
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:http://www.hongdianyidai.com");
		/*
		List<Map<String, Object>> list = cjService.queryCjListAll(params);
		List<String> gyhList = new ArrayList<>();//员工信息
		List<String> xmList = new ArrayList<>();//姓名信息
		List<String> gwList = new ArrayList<>();//岗位信息
		List<String> picList = new ArrayList<>();//照片路径信息
		List<Map<String,Object>> zjLevel1List = new ArrayList<>();//已中奖人员总信息-一等奖
		List<Map<String,Object>> zjLevel2List = new ArrayList<>();//已中奖人员总信息-二等奖
		List<Map<String,Object>> zjLevel3List = new ArrayList<>();//已中奖人员总信息-三等奖
		for (Map<String, Object> map : list) {
			File file=new File(ROOT_PATH + MapUtils.getString(map, "PIC_PATH"));
			if (file.exists()) {
				map.put("ISBE", true);
			}else {
				map.put("ISBE", false);
			}
			gyhList.add(MapUtils.getString(map, "GYH",""));
			xmList.add(MapUtils.getString(map, "REAL_NAME",""));
			gwList.add(MapUtils.getString(map, "RYGW",""));
			picList.add(MapUtils.getString(map, "PIC_PATH",""));
			
			String level = MapUtils.getString(map, "ZJ_LEVEL","");
			if(StringUtils.isNotEmpty(level)){
				level = level.toUpperCase();
				if("LEVEL_1".equals(level)){
					zjLevel1List.add(map);
				}else if("LEVEL_2".equals(level)){
					zjLevel2List.add(map);
				}else if("LEVEL_3".equals(level)){
					zjLevel3List.add(map);
				}
			}
			
		}
		JSONObject result = new JSONObject();
		result.put("list", list);
		result.put("gyhList", gyhList);
		result.put("xmList", xmList);
		result.put("gwList", gwList);
		result.put("picList", picList);
		result.put("zjLevel1List",zjLevel1List);
		result.put("zjLevel2List",zjLevel2List);
		result.put("zjLevel3List",zjLevel3List);
		result.put("zjLevel3count",zjLevel3List.size());
		System.out.println(result.toString());
		mv.addObject("result", result);
		mv.setViewName("/other/nh2017/index.htm");*/
		return mv;
	}

	@RequestMapping(value = "/readyImg.do")
	public void readyImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
		FileInputStream  fileIs = new FileInputStream (ROOT_PATH + "/index-1.jpg");
		int i = fileIs.available(); // 得到文件大小
		byte data[] = new byte[i];
		fileIs.read(data); // 读数据
		response.setContentType("image/*"); // 设置返回的文件类型
		OutputStream outStream = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
		outStream.write(data); // 输出数据
		outStream.flush();
		outStream.close();
		fileIs.close();
	}
	
	@RequestMapping(value = "/showImg")
	public void ShowImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
		 request.setCharacterEncoding("UTF-8");
		String imgFile = request.getParameter("imgFile"); // 文件名
		imgFile = new String(imgFile.getBytes("ISO8859-1"),"UTF-8");
		imgFile = URLDecoder.decode(imgFile,"UTF-8");
		FileInputStream fileIs = null;
		try {
			fileIs = new FileInputStream(ROOT_PATH + "/" + imgFile);
		} catch (Exception e) {
			logger.error("系统找不到图像文件：" + ROOT_PATH + "/" + imgFile);
			return;
		}
		int i = fileIs.available(); // 得到文件大小
		byte data[] = new byte[i];
		fileIs.read(data); // 读数据
		response.setContentType("image/*"); // 设置返回的文件类型
		OutputStream outStream = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
		outStream.write(data); // 输出数据
		outStream.flush();
		outStream.close();
		fileIs.close();
	}
	
	@RequestMapping(value = "/showImgByGyh")
	public void showImgByGyh(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("UTF-8");
		String gyh = RequestUtils.getStringParameter(request, "gyh");
		if(StringUtils.isEmpty(gyh)){
			logger.error("gyh: " + gyh + "的图片,不存在");
			return;
		}
		Map userInfoMap = this.cjService.getZnnhUserinfoByGyh(gyh);
		String imgFile = MapUtils.getString(userInfoMap, "PIC_PATH",""); // 文件名
		FileInputStream fileIs = null;
		try {
			fileIs = new FileInputStream(ROOT_PATH + "/" + imgFile);
		} catch (Exception e) {
			logger.error("系统找不到图像文件：" + ROOT_PATH + "/" + imgFile);
			return;
		}
		int i = fileIs.available(); // 得到文件大小
		byte data[] = new byte[i];
		fileIs.read(data); // 读数据
		response.setContentType("image/*"); // 设置返回的文件类型
		OutputStream outStream = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
		outStream.write(data); // 输出数据
		outStream.flush();
		outStream.close();
		fileIs.close();
	}
	
	@RequestMapping(value = "/getZnnhCjjg")
	public void getZnnhCjjg(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String num = RequestUtils.getStringParameter(request, "num");//中奖个数
		String level = RequestUtils.getStringParameter(request, "level");//开奖级别
		JSONObject result = new JSONObject();
		if(StringUtils.isNotEmpty(num) && StringUtils.isNotEmpty(level)){
			level = level.toUpperCase();//不区分大小写
			int yzjCount = this.cjService.getYzjCount(level);
			if(OtherConstant.IS_START_ZNNH_LIMIT){//是否开启得奖个数验证
				if("LEVEL_1".equals(level)){
					if(yzjCount >= OtherConstant.MAX_NUM_LEVEL_1){//超过或者等于最大个数
						result.put("success", true);
						result.put("is_suc", false);
						result.put("msg", "一等奖抽奖已结束！");
						serialize(request, response, result);
						return;
					}
				}else if("LEVEL_2".equals(level)){
					if(yzjCount >= OtherConstant.MAX_NUM_LEVEL_2){//超过或者等于最大个数
						result.put("success", true);
						result.put("is_suc", false);
						result.put("msg", "二等奖抽奖已结束！");
						serialize(request, response, result);
						return;
					}
				}else if("LEVEL_3".equals(level)){
					if(yzjCount >= OtherConstant.MAX_NUM_LEVEL_3){//超过或者等于最大个数
						result.put("success", true);
						result.put("is_suc", false);
						result.put("msg", "三等奖抽奖已结束！");
						serialize(request, response, result);
						return;
					}
				}
			}
			List<Map<String,Object>> zjList = this.cjService.getZjList(num,level);
			result.put("success", true);
			result.put("is_suc", true);
			result.put("msg", "抽奖成功!");
			result.put("zjList", zjList);
			//获取最新三等奖信息
			int yzj_level3_count = this.cjService.getYzjCount("LEVEL_3");
			result.put("level_3_count", yzj_level3_count);
		}else{
			result.put("success", false);
			result.put("msg", "您的操作不正确!");
		}
		serialize(request, response, result);
	}

	/**
	 * 验证年会管理员密码
	 * */
	@RequestMapping(value = "/checkUserSafe")
	public void checkUserSafe(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String yhm = RequestUtils.getStringParameter(request, "yhm");
		String password = RequestUtils.getStringParameter(request, "password");
		JSONObject result = new JSONObject();
		if("znnh".equals(yhm)){
			boolean isVerifySuc = this.cjService.checkUserSafe(password);
			if(isVerifySuc){
				request.getSession().setAttribute("isUserPass", true);
				result.put("success", true);
				result.put("msg", "验证通过");
			}else{
				result.put("success", false);
				result.put("msg", "用户名或密码不正确！");
			}
		}else{
			result.put("success", false);
			result.put("msg", "用户名不正确！");
		}
		serialize(request, response, result);
	}
}
